# -*- coding: utf-8 -*-

#利用Python的Playwright库中的Trace Viewer功能进行自动化测试追踪。
# 当遇到不稳定bug时，Trace Viewer能帮助复现和分析问题。通过browser_context.tracing API记录测试跟踪，保存为trace.zip文件。
# 在GUI工具中，可以查看每个操作前后页面状态，检查日志、源和网络。
#执行命令： playwright show-trace trace.zip


from playwright.sync_api import Playwright, sync_playwright, expect

def run(playwright: Playwright) -> None:

    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()

    # 配置追踪开始
    context.tracing.start(screenshots=True, snapshots=True, sources=True)

    page.goto('http://192.168.7.102/users/sign_in')
    page.get_by_label("Username or email").fill("yangxc@it.lianfa.cn")
    page.get_by_label("Password").fill("victal2422")
    page.get_by_role("button", name="Sign in").click()

    # 结束追踪的地方，添加 tracing 的结束配置。
    context.tracing.stop(path="trace.zip")

    context.close()
    browser.close()

if __name__ == '__main__':

    with sync_playwright() as playwright:
        run(playwright)

